package com.qunar.qcf.handler;

import java.lang.reflect.Method;

import com.qunar.qcf.core.session.IoSession;
import com.qunar.qcf.handler.transport.ServerSender;
import com.qunar.qcf.handler.transport.Transport;

public final class IoServerHandler extends AbstractServerHandler {
//	private final Logger logger = LoggerFactory.getLogger(IoServerHandler.class);

	public IoServerHandler() {
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		//logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " Server端连接ip地址为>>>:" + session.getRemoteAddress());
	}

	@Override
	public void sessionClosed(IoSession session) {
		//logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " Server端连接ip地址为<<<:" + session.getRemoteAddress());
	}

	@Override
	public void processor(IoSession session, Object message) {
		Transport transport = (Transport) message;
		
		InvokeHandler handler = handlers.get(transport.getClazz());

		Object[] params = (Object[]) transport.getMessage();
		Object ret = null;
		try {
			Method method = initMethods.get(transport.getClazz() + transport.getMethod());
			if (method == null) {
			} else {
				ret = method.invoke(handler, params);
			}
			ServerSender sender = new ServerSender(session, transport);
			sender.send(ret);
		} catch (Exception e) {
			throw new IllegalAccessError(e.getMessage());
		}
	}
}
